package com.yanchangapp.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yanchangapp.model.cache.UserCache;
import com.zxl.baselib.util.time.TimeUtil;
import com.zxl.baselib.util.ui.ResHelper;
import com.yanchangapp.R;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.MailType;
import com.yanchangapp.model.bean.MailItemBean;
import com.yanchangapp.model.bean.MailUserBean;
import com.yanchangapp.util.TextHighHelper;

import java.util.List;

/**
 * Created by apple on 2019/7/16.
 * description: 邮件搜索的Adapter类
 */
public class MailSearchAdapter extends BaseQuickAdapter<MailItemBean, BaseViewHolder> {
    private String[] mKeywordsArray = new String[1];
    public MailSearchAdapter(int layoutResId, @Nullable List<MailItemBean> data) {
        super(layoutResId, data);
    }

    public void setCurrentKeyWords(String currentKeyWords){
        mKeywordsArray[0] = currentKeyWords;
    }

    @Override
    protected void convert(BaseViewHolder helper, MailItemBean item) {
        // 设置为查看状态
        helper.getView(R.id.rlEditStyle).setVisibility(View.GONE);
        helper.getView(R.id.rlLookStyle).setVisibility(View.VISIBLE);
        View ivIcon =  helper.getView(R.id.blueDot);
        // 设置已读未读的图标展示,紧接着设置颜色咯
        if (item.getSeenFlag() == AppConstant.MAIL_UN_READ){
            if (item.getMailbox() == MailType.SENT.getMailDetail().getMailBoxType() ||
                    item.getMailbox() == MailType.DRAFT.getMailDetail().getMailBoxType()){
                ivIcon.setVisibility(View.GONE);
                helper.getView(R.id.tvMailSender).setSelected(true);
                helper.getView(R.id.rlLookStyle).setSelected(true);
                helper.getView(R.id.tvMailTheme).setSelected(true);
                helper.getView(R.id.tvMailContent).setSelected(true);
            }else {
                ivIcon.setVisibility(View.VISIBLE);
                helper.getView(R.id.tvMailSender).setSelected(false);
                helper.getView(R.id.rlLookStyle).setSelected(false);
                helper.getView(R.id.tvMailTheme).setSelected(false);
                helper.getView(R.id.tvMailContent).setSelected(false);
            }

        }else {
            ivIcon.setVisibility(View.GONE);
            helper.getView(R.id.tvMailSender).setSelected(true);
            helper.getView(R.id.rlLookStyle).setSelected(true);
            helper.getView(R.id.tvMailTheme).setSelected(true);
            helper.getView(R.id.tvMailContent).setSelected(true);
        }
        // 设置姓名
        helper.setText(R.id.tvMailSender, TextHighHelper.matcherSearchContent(getSendName(item.getToUsers(),item.getMailbox(),item.getSenderNickname()),
                mKeywordsArray));
        // 设置姓名的头标...
        helper.setText(R.id.tvTextSingleName,getSingleIconText(item.getToUsers(),item.getMailbox(),item.getSenderNickname()));
        // 设置主题
        helper.setText(R.id.tvMailTheme,TextHighHelper.matcherSearchContent(item.getSubject(),mKeywordsArray));
        // 设置内容
        helper.setText(R.id.tvMailContent,item.getContent());
        // 设置时间
        helper.setText(R.id.tvMailTime, TimeUtil.getTimeDes(item.getMailTime()));
    }

    private String getSendName(List<MailUserBean> mList , int mailBox, String sendName){
        String targetName = "";
        if (mailBox == MailType.SENT.getMailDetail().getMailBoxType() ||
                mailBox == MailType.DRAFT.getMailDetail().getMailBoxType()  ){
            if (mList != null && mList.size() != 0){
                targetName = mList.get(0).getNickname();
                for (int i=1;i<mList.size();i++){
                    targetName = targetName + ","+mList.get(i).getNickname();
                }
                return targetName;
            }
            return "(无收件人)";
        }
        return sendName;
    }

    private String getSingleIconText(List<MailUserBean> mList , int mailBox, String sendName){
        String targetName = "";
        if (mailBox == MailType.SENT.getMailDetail().getMailBoxType() ||
                mailBox == MailType.DRAFT.getMailDetail().getMailBoxType()  ){
            if (mList != null && mList.size() != 0){
                targetName = mList.get(0).getNickname();
                if (TextUtils.isEmpty(targetName)){
                    return " ";
                }
                return targetName.substring(0,1);
            }
            return " ";
        }
        if (TextUtils.isEmpty(sendName)){
            return " ";
        }
        if (TextUtils.isEmpty(sendName)){
            return  " ";
        }
        return  sendName.substring(0,1);
    }
}
